// pages/video/video.js
import request from "../../static/utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoGroupList:[],
        navId:'', //导航标识
        videoList:[],//视频列表数据
        videoId:[],//视频id标识
        videoUpdateTime:[],//视频播放时间
        isTriggered:false,//刷新状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.showLoading({
            title: '正在加载',
        });
        this.getVideoGroupListData();
        this.getVideoList(this.data.navId);
    },
    //改变导航条
    changeNav(event){
        let navId = event.currentTarget.id;
        this.setData({
            navId:navId*1,
            videoList:[]
        })
        //切换时加载
        wx.showLoading({
            title: '正在加载',
        });
          
        this.getVideoList(this.data.navId);
    },
    //点击播放回调
    handlePlay(event){
        //js单例模式
        // 需要创建多个对象的场景下，通过一个变量接受，始终保持只有一个对象

        //获取控制video标签的id
        let vid = event.currentTarget.id;
        //关闭上一个播放的视频
        // this.vid !== vid && this.videoContext && this.videoContext.stop();
        // this.vid = vid;
        // console.log(vid);
        this.setData({
            videoId:vid
        });
        // 创建控制video标签的实例对象
        this.videoContext = wx.createVideoContext(vid);
        //判断当前视频是否播放过，有则跳转到指定位置
        let {videoUpdateTime} = this.data;
        let videoItem = videoUpdateTime.find(item => item.vid === vid)
        if(videoItem){
            this.videoContext.seek(videoItem.currentTime);
        }
        // this.videoContext.play();
          
    },
    //监听播放进度的回调
    handleTimeUpdate(event){
        let videoTimeObj = {
            vid:event.currentTarget.id,
            currentTime:event.detail.currentTime
        };
        let {videoUpdateTime} = this.data;
        let videoItem = videoUpdateTime.find(item => item.vid === videoTimeObj.vid);
        if(videoItem){
            videoItem.currentTime = event.detail.currentTime;
        }else{
            videoUpdateTime.push(videoTimeObj)
        }
        this.setData({
            videoUpdateTime
        })
    },
    handleEnded(event){
        //移除播放对象
        let {videoUpdateTime} = this.data;
        let index = videoUpdateTime.findIndex(item => item.vid === event.currentTarget.id);
        videoUpdateTime.splice(index,1);
        this.setData({
            videoUpdateTime
        })
    },
    // 下拉刷新
    handleRef(){
        this.setData({
            isTriggered:true
        });
        this.getVideoList(this.data.navId);
    },
    //上拉触底
    handleToLower(){
        //数据分页：1.后端分页  2.前端分页
        //由于网易云没有对应的接口
        //改为提示到头
        wx.showToast({
            title: '已经到头了',
            icon:'none',
            duration: 1000,
        });
          
    },

    //获取导航数据
    async getVideoGroupListData(){
        let videoGroupData=await request('/video/group/list');
        this.setData({
            videoGroupList:videoGroupData.data.slice(0,14),
            navId:videoGroupData.data[0].id
        });
        //获取视频列表数据
        this.getVideoList(this.data.navId);
    },
    //获取视频列表数据
    async getVideoList(navId){
        if(!navId){
            return;
        }
        // console.log(navId);
        let videoListData = await request('/video/group', {id:navId});
        // console.log(videoListData);
        //关闭提示框
        wx.hideLoading();
          
        let index = 0;
        let videoList = videoListData.datas.map(item =>{
            item.id = index++;
            return item;
        })
        this.setData({
            videoList,
            isTriggered:false
        })
    },
    toSearch(){
        wx.navigateTo({
            url: '/pages/search/search',
        })
          
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function ({from}) {
        if(from==="button"){
            return {
                title:'快来康康！',
                page:'/pages/video/video',
                imageUrl:'/static/images/personal/nightime.jpg'
            };
        }else{
            return {
                title:'快来康康！',
                page:'/pages/video/video',
                imageUrl:'/static/images/personal/nightime.jpg'
            };
        }
        
    }
})